package com.example.starseas_phone;

import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.lljjcoder.citypickerview.widget.CityPickerView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class Modify_the_Stu extends AppCompatActivity {

    private EditText modify_the_StuName;
    private EditText modify_the_StuNational;
    private RadioButton modify_the_stusex1;
    private RadioButton modify_the_stusex2;
    private EditText modify_the_StuAge;
    private Spinner modify_the_StuPoliticalLandscape;
    private TextView modify_the_StuNativePlace;
    private EditText modify_the_Stucarid;
    private EditText modify_the_StuPhone;
    private EditText modify_the_StuEmail;
    private Spinner modify_the_StuClassId;
    private TextView modify_the_StubackTime;
    private Button modify_the_stu_btn;

    private List<String> classlist =new ArrayList<String>();
    private String selectClassid="";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_the_stu);
        initclassid();
        initid();
        initview();
        modify_the_stu_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tijiao();
            }
        });
        modify_the_StubackTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDatePickerDialog(modify_the_StubackTime);
            }
        });
        modify_the_StuNativePlace.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CityPickerView cityPickerView = new CityPickerView(Modify_the_Stu.this);
                cityPickerView.setOnCityItemClickListener(new CityPickerView.OnCityItemClickListener() {
                    @Override
                    public void onSelected(String... citySelected) {
                        //省份
                        String province = citySelected[0];
                        //城市
                        String city = citySelected[1];
                        //区县
                        String district = citySelected[2];
                        //邮编
                        String code = citySelected[3];
                        //控件自己写两个edittext或者Xedittext用来显示文本代替下面两个个控件ID
                        //address_new_address用来展示地址
                        //address_new_zipcode用来展示邮编
                        modify_the_StuNativePlace.setText(province+"-"+ city+"-"+ district);
                    }
                });
                cityPickerView.show();

            }
        });

        modify_the_StuClassId.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                selectClassid = classlist.get(i);
                modify_the_StuClassId.setSelection(i,true);
                Log.d("selectClassid",selectClassid);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

    }

    private void initid(){

        modify_the_StuName = (EditText) findViewById(R.id.modify_the_StuName);
        modify_the_StuNational = (EditText) findViewById(R.id.modify_the_StuNational);
        modify_the_stusex1 = (RadioButton) findViewById(R.id.modify_the_stusex1);
        modify_the_stusex2 = (RadioButton) findViewById(R.id.modify_the_stusex2);
        modify_the_StuAge = (EditText) findViewById(R.id.modify_the_StuAge);
        modify_the_StuPoliticalLandscape = (Spinner) findViewById(R.id.modify_the_StuPoliticalLandscape);
        modify_the_StuNativePlace = (TextView) findViewById(R.id.modify_the_StuNativePlace);
        modify_the_Stucarid = (EditText) findViewById(R.id.modify_the_Stucarid);
        modify_the_StuPhone = (EditText) findViewById(R.id.modify_the_StuPhone);
        modify_the_StuEmail = (EditText) findViewById(R.id.modify_the_StuEmail);
        modify_the_StuClassId = (Spinner) findViewById(R.id.modify_the_StuClassId);
        modify_the_StubackTime = (TextView) findViewById(R.id.modify_the_StubackTime);
        modify_the_stu_btn = (Button) findViewById(R.id.modify_the_stu_btn);

    }

    private void initview(){
        //修改名字
        modify_the_StuName.setText(Sto.students.getStuName());
        //修改名族
        modify_the_StuNational.setText(Sto.students.getNational());
        //修改性别
        if (Sto.students.getStuSex().equals("男")){
            modify_the_stusex1.setChecked(true);
        }else {
            modify_the_stusex2.setChecked(true);
        }

        //修改年龄
        modify_the_StuAge.setText(Sto.students.getStuAge()+"");
        //修改政治面貌
        if (Sto.students.getPoliticalLandscape().equals("群众")){
            modify_the_StuPoliticalLandscape.setSelection(0);
        }else if(Sto.students.getPoliticalLandscape().equals("团员")){
            modify_the_StuPoliticalLandscape.setSelection(1);
        }else if(Sto.students.getPoliticalLandscape().equals("党员")){
            modify_the_StuPoliticalLandscape.setSelection(2);
        }
        //修改籍贯
        modify_the_StuNativePlace.setText(Sto.students.getNativePlace());
        //修改身份证
        modify_the_Stucarid.setText(Sto.students.getStuCarid());
        //修改手机号
        modify_the_StuPhone.setText(Sto.students.getStuPhone());
        //修改邮箱
        modify_the_StuEmail.setText(Sto.students.getStuEmail());

        //修改开学时间
        modify_the_StubackTime.setText(Sto.students.getStubackTime());
    }

    private void tijiao(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("StuId",Sto.students.getStuId());
                    jsonObject.put("StuName",modify_the_StuName.getText().toString());
                    jsonObject.put("National",modify_the_StuNational.getText().toString());
                    jsonObject.put("StuAge",Integer.parseInt(modify_the_StuAge.getText().toString()));
                    jsonObject.put("StubackTime",modify_the_StubackTime.getText().toString());
                    jsonObject.put("StuCarid",modify_the_Stucarid.getText().toString());
                    jsonObject.put("StuEmail",modify_the_StuEmail.getText().toString());
                    jsonObject.put("StuPhone",modify_the_StuPhone.getText().toString());
                    String sex;
                    if (modify_the_stusex1.isChecked()){
                        sex = "男";
                    }else {
                        sex = "女";
                    }
                    jsonObject.put("StuSex",sex);
                    jsonObject.put("ClassId",selectClassid);
                    String PoliticalLandscape;
                    if (modify_the_StuPoliticalLandscape.getSelectedItemId()==0){
                        PoliticalLandscape="群众";
                    }else if(modify_the_StuPoliticalLandscape.getSelectedItemId()==1){
                        PoliticalLandscape="团员";;
                    }else{
                        PoliticalLandscape="党员";
                    }
                    jsonObject.put("PoliticalLandscape",PoliticalLandscape);
                    jsonObject.put("NativePlace",modify_the_StuNativePlace.getText().toString());

                    JSONArray jsonArray = new JSONArray();
                    jsonArray.put(0,jsonObject);
                    JSONObject jsonObjects = new JSONObject();
                    jsonObjects.put("token",Sto.students.getToken());
                    jsonObjects.put("InfoJSON",jsonArray);
                    jsonObjects.put("ApplicationType",2);
                    jsonObjects.put("Status",1);
                    Log.d("修改数据",jsonObjects.getString("InfoJSON"));
                    Log.d("数据",jsonObjects.toString());
                    MediaType mediaType = MediaType.parse("application/json");
                    RequestBody requestBody = RequestBody.create(mediaType,"");
                    String result = URLEncoder.encode(jsonObjects.getString("InfoJSON"), "utf-8");
                    String urls = Sto.NetIP+"/api/TheApplicationForm/InsertApplicationForm?token="+Sto.students.getToken()+"&InfoJSON="+result+"&ApplicationType=2&Status=1";
                    Log.d("url",urls);
                    Request request1 = new Request.Builder()
                            .url(urls)
                            .post(requestBody)
                            .build();
                    Response response = client.newCall(request1).execute();

                    String results = response.body().string();
                    JSONObject jsonObject1 = new JSONObject(results);
                    String code = jsonObject1.getString("code");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (code.equals("200")){
                                Toast.makeText(Modify_the_Stu.this,"申请成功",Toast.LENGTH_SHORT).show();
                                finish();
                            }else {
                                Toast.makeText(Modify_the_Stu.this,"申请失败",Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    //弹出选择日期的对话框
    private void showDatePickerDialog(final TextView textView){
        Calendar c = Calendar.getInstance();
        new DatePickerDialog(Modify_the_Stu.this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
                textView.setText(i+"-"+(i1+1)+"-"+i2);
            }
        },c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH)).show();
    }

    private void initclassid(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    Request request1 = new Request.Builder()
                            .url(Sto.NetIP+"/api/Class/GetAllClasses")
                            .get()
                            .build();
                    Response response = client.newCall(request1).execute();
                    String results = response.body().string();
                    JSONObject jsonObject1 = new JSONObject(results);
                    String message = jsonObject1.getString("message");
                    JSONArray jsonArray = new JSONArray(message);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        String ClassId = jsonObject.getString("ClassId");
                        classlist.add(ClassId);
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ArrayAdapter<String> adapter = new ArrayAdapter<String>(Modify_the_Stu.this,R.layout.classid_item, R.id.text,classlist);
                            modify_the_StuClassId.setAdapter(adapter);
                            //修改班级
                            int position = 3;
                            for (int i = 0; i < classlist.size(); i++) {
                                if (classlist.get(i).equals(Sto.students.getStuClassId())){
                                    position = i;
                                }
                            }
                            modify_the_StuClassId.setSelection(position,true);
                            Log.d("position",position+"");
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

}